package people.paper.tv.activity.home.view.impl;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.skyworth.util.Util;

import people.paper.tv.R;
import people.paper.tv.activity.home.view.HomeView;
import people.paper.tv.activity.home.view.IFragementCallback;

public class HomeViewImpl implements HomeView {
    public static final int FRAGMENT_ID = 1000;
    private FrameLayout root;
    private TitleBar titleBar;
    public Context context;
    IFragementCallback fragementCallback;
    View focus;

    public HomeViewImpl(Context context) {
        this.context = context;
        root = new FrameLayout(context);
        root.setBackgroundResource(R.mipmap.bg);

        ImageView view = new ImageView(context);
        view.setImageResource(R.mipmap.paper_logo);
        root.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.RIGHT));

        initTitle();
        initFragmentContent();
    }

    private void initTitle() {
        titleBar = new TitleBar(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, Util.Div(140));
        params.leftMargin = Util.Div(87);
        params.topMargin = Util.Div(20);
        root.addView(titleBar, params);
    }

    private void initFragmentContent(){
        focus = new View(context);
        focus.setFocusable(true);
        focus.setFocusableInTouchMode(true);
        focus.setVisibility(View.GONE);
        root.addView(focus);
        focus.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                return true;
            }
        });

        FrameLayout content = new FrameLayout(context);
        content.setId(HomeViewImpl.FRAGMENT_ID);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        root.addView(content, params);
    }

    @Override
    public void placeFocus(boolean hasFocus){
        if(hasFocus){
            focus.setVisibility(View.VISIBLE);
            focus.requestFocus();
        }else {
            focus.setVisibility(View.GONE);
        }
    }

    @Override
    public void setFragementCallback(IFragementCallback callback){
        fragementCallback = callback;
        titleBar.setFragementCallback(fragementCallback);
    }

    @Override
    public View getView() {
        return root;
    }

    @Override
    public int getFragmentId() {
        return FRAGMENT_ID;
    }

    @Override
    public void changeFocusToBar(int id) {
        refreshFocusTitle(id);
    }

    @Override
    public void changeSelectedToBar(int id) {
        refreshSelectedTitle(id);
    }

    public void refreshFocusTitle(int id){
        titleBar.setFocusBar(id);
        titleBar.refreshFocusTitle(id);
    }

    public void refreshSelectedTitle(int id){
        titleBar.refreshSelectedTitle(id);
    }
}
